﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;
using System.Reflection;
using System.Globalization;

namespace Directory.Web
{
	/// <summary>
	/// WebSettings 的摘要说明
	/// </summary>
	[Serializable]
	public sealed class WebSettings
	{
		private WebSettings()
		{
			WebUtil.ResetSettingsHandler -= ResetEvent;
			WebUtil.ResetSettingsHandler += ResetEvent;
		}

		private static WebSettings _instance;

		public static WebSettings GetInstance()
		{
			if (_instance == null)
			{
				LoadSettings();
			}
			return _instance;
		}

		#region helper

		private static void LoadSettings()
		{
			_instance = new WebSettings();
			_instance.Load();
		}

		private void Load()
		{
			string fileLocation = ConfigurationManager.AppSettings["WebSettingsFileLocation"];
			string absolutePath = System.Web.HttpContext.Current.Server.MapPath(fileLocation);

			try
			{
				XmlDocument doc = new XmlDocument();
				doc.Load(absolutePath);
				Type ty = this.GetType();
				ty.GetFields();
				foreach (XmlNode node in doc.LastChild.ChildNodes)
				{
					PropertyInfo p = ty.GetProperty(node.Name);
					p.SetValue(this, Convert.ChangeType(node.InnerText, p.PropertyType, CultureInfo.CurrentCulture), null);
				}
			}
			catch
			{
			}
		}

		private void Save()
		{
			string fileLocation = ConfigurationManager.AppSettings["WebSettingsFileLocation"];
			string absolutePath = System.Web.HttpContext.Current.Server.MapPath(fileLocation);

			XmlWriterSettings writersettings = new XmlWriterSettings();
			writersettings.Indent = true;
			writersettings.Encoding = System.Text.Encoding.UTF8;
			writersettings.OmitXmlDeclaration = true;

			using (XmlWriter writer = XmlWriter.Create(absolutePath, writersettings))
			{
				Type ty = this.GetType();
				PropertyInfo[] properties = ty.GetProperties();


				writer.WriteStartElement("settings");

				foreach (PropertyInfo p in properties)
				{
					writer.WriteElementString(p.Name, (p.GetValue(this, null) == null ? "" : p.GetValue(this, null).ToString()));
				}

				writer.WriteEndElement();
			}
		}

		public static void SaveSettings()
		{
			if (_instance != null)
			{
				_instance.Save();
			}
		}

		public static void ResetEvent(object sender, EventArgs e)
		{
			_instance = null;
		}

		#endregion

		#region Properties

		private int _rootCategoryId;
		public int RootCategoryId
		{
			get { return _rootCategoryId; }
			private set { _rootCategoryId = value; }
		}

		private string _siteName;
		public string SiteName
		{
			get { return _siteName; }
			set { _siteName = value; }
		}

		private string _hostName;
		public string HostName
		{
			get { return _hostName; }
			set { _hostName = value; }
		}

		private string _customRole;
		public string CustomRole
		{
			get { return _customRole; }
			private set { _customRole = value; }
		}

		private string _rootCategoryAdminRole;
		public string RootCategoryAdminRole
		{
			get { return _rootCategoryAdminRole; }
			private set { _rootCategoryAdminRole = value; }
		}

		private string _categoryAdminRole;
		public string CategoryAdminRole
		{
			get { return _categoryAdminRole; }
			private set { _categoryAdminRole = value; }
		}

		private string _securityAdminRole;
		public string SecurityAdminRole
		{
			get { return _securityAdminRole; }
			private set { _securityAdminRole = value; }
		}

		private string _siteSettingsAdminRole;
		public string SiteSettingsAdminRole
		{
			get { return _siteSettingsAdminRole; }
			set { _siteSettingsAdminRole = value; }
		}

		private int _categoryViewPageSizeForAdmin;
		public int CategoryViewPageSizeForAdmin
		{
			get { return _categoryViewPageSizeForAdmin; }
			private set { _categoryViewPageSizeForAdmin = value; }
		}

		private int _itemViewPageSizeForAdmin;
		public int ItemViewPageSizeForAdmin
		{
			get { return _itemViewPageSizeForAdmin; }
			set { _itemViewPageSizeForAdmin = value; }
		}

		private int _userViewPageSize;
		public int UserViewPageSize
		{
			get { return _userViewPageSize; }
			set { _userViewPageSize = value; }
		}

		private int _messageViewPageSizeForAdmin;
		public int MessageViewPageSizeForAdmin
		{
			get { return _messageViewPageSizeForAdmin; }
			set { _messageViewPageSizeForAdmin = value; }
		}

		private int _itemViewPageSize;
		public int ItemViewPageSize
		{
			get { return _itemViewPageSize; }
			set { _itemViewPageSize = value; }
		}

		private string _anonymousUserName;
		public string AnonymousUserName
		{
			get { return _anonymousUserName; }
			private set { _anonymousUserName = value; }
		}

		private string _nickNameCookieKey;
		public string NickNameCookieKey
		{
			get { return _nickNameCookieKey; }
			private set { _nickNameCookieKey = value; }
		}

		private int _searchItemPageSize;
		public int SearchItemPageSize
		{
			get { return _searchItemPageSize; }
			set { _searchItemPageSize = value; }
		}

		private int _messageBoardPageSize;
		public int MessageBoardPageSize
		{
			get { return _messageBoardPageSize; }
			set { _messageBoardPageSize = value; }
		}

		private int _recommandItemsPageSize;
		public int RecommandItemsPageSize
		{
			get { return _recommandItemsPageSize; }
			set { _recommandItemsPageSize = value; }
		}

		private int _recommandCategoriesPageSize;
		public int RecommandCategoriesPageSize
		{
			get { return _recommandCategoriesPageSize; }
			set { _recommandCategoriesPageSize = value; }
		}

		private int _recentAddedItemsCount;
		public int RecentAddedItemsCount
		{
			get { return _recentAddedItemsCount; }
			set { _recentAddedItemsCount = value; }
		}

		private int _uncheckedItemsPageSize;
		public int UncheckedItemsPageSize
		{
			get { return _uncheckedItemsPageSize; }
			set { _uncheckedItemsPageSize = value; }
		}

		#endregion

	}
}
